﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;
using Com.CMS263.Util;

namespace Com.CMS263.DAO
{
    public static class LogicUtil
    {
       
        /// <summary>
        /// 添加一条日志
        /// </summary>
        /// <param name="msg"></param>
        public static void AddALog(string msg)
        {
            string ip = "";
            try
            {
                if (HttpContext.Current != null && HttpContext.Current.Request != null)
                    ip = HttpContext.Current.Request.UserHostAddress;
              string  logPath=HttpContext.Current.Server.MapPath("/Logs/"+DateTime.Now.ToString("yyyy-MM-dd")+"/");
              if (!Directory.Exists(logPath))
              {
                  Directory.CreateDirectory(logPath);
              }
              string fileName = DateTime.Now.Hour.ToString() + ".log";
              if (!File.Exists(logPath + fileName ))
              {
                  File.Create(logPath + fileName);
              }
              FileUtil.AppendToFile(logPath + fileName, msg+System.Environment.NewLine);
            }
            catch (Exception ex)
            {
               // log.Error("", ex);
            }
        }
        /// <summary>
        /// 保存实体(保存为Xml)
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="obj">对象</param>
        public static void SaveObj(string fileName, object obj)
        {
            // TODO: implement
            FileStream fs = null;

            try
            {
                if (File.Exists(fileName))
                    File.Delete(fileName);
                fs = new FileStream(fileName, FileMode.CreateNew, FileAccess.Write);
                System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(obj.GetType());
                xs.Serialize(fs, obj);
            }
            catch (System.Exception ce)
            {
                //Console.WriteLine(ce.Message);
                throw ce;
            }
            finally
            {
                if (fs != null)
                    fs.Close();
            }
        }

        /// <summary>
        /// 加载实体（从Xml中）
        /// </summary>
        /// <param name="fileName">文件全路径</param>
        /// <param name="_type">类型</param>
        /// <returns></returns>
        public static object LoadObj(string fileName, Type _type)
        {
            // TODO: implement
            FileStream fs = null;
            object ResultObj = null;

            try
            {
                fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(_type);
                ResultObj = xs.Deserialize(fs);
            }
            catch (System.Exception ce)
            {
                //Console.WriteLine(ce.Message);
                throw ce;
            }
            finally
            {
                if (fs != null)
                    fs.Close();
            }
            return ResultObj;
        }
    }
}
